
Partial Class Budget_ExpenditureReportView
    Inherits System.Web.UI.Page
    Dim lblBalance As Label

 

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load

        Dim sqlDataSource As New SqlDataSource
        Dim parmParticipantId As New Parameter
        Dim parmExpenditureReportId As New Parameter
        Dim args As New DataSourceSelectArguments
        Dim Participant As New Cdm.DataCore.Entities.Participant()

        SetupJavascript()
        ParticipantNameLabel.Visible = False

        If Not Page.IsPostBack Then
            UserAuthorization.CheckPagePermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseMonthlyStatement, "VIEW")
            PopulateSpendingPlanList()
            If Not Request("id") Is Nothing Then
                ApprovedSpendingPlanDropDownList.SelectedValue = Request("id")
                Me.MatchSpendingPlanDateRange.Checked = True
                GetActivity()
            End If
        End If

        EntityController.SetSubNavTabsFinancial(Me.EditCaseSubNav2)
        MonthlyStatementLabel.Text = "View " + EntityController.ReplaceDisplayName("Monthly Statement")
        Me.Title = "View " + EntityController.ReplaceDisplayName("Monthly Statement")
        EntityController.SetFinancialTab(Me.FinancialSubName, Me.EditCaseSubNav2)

        'Dim connectionString As String = ConfigurationManager.ConnectionStrings("SpendingPlanData").ConnectionString
        Dim connectionString As String = EntityController.AppConnectString

        Participant = EntityController.GetParticipant()
        If Participant Is Nothing Then
            maindiv.Visible = False
            ParticipantNameLabel.Text = "There is no participant associated with this user."
            ParticipantNameLabel.Visible = True
        Else
            ParticipantName.Text = Participant.DisplayLastNameFirst()
            ParticipantNameLabel.Text = "There is no spending activity during this time for " + Participant.DisplayLastNameFirst()
        End If

    End Sub

    Private Sub SetupJavaScript()
        EntityController.JavaScriptAddChildDiv(Me.maindiv.ClientID, 100)
    End Sub

    Protected Sub PopulateSpendingPlanList()
        Dim participant As Cdm.DataCore.Entities.Participant = EntityController.GetParticipant()
        ApprovedSpendingPlanDropDownList.DataSource = Cdm.DataCore.Budget.ApprovedSpendingPlan.ListByParticipant(participant.ParticipantId)
        ApprovedSpendingPlanDropDownList.DataTextField = "DateRangeDescription"
        ApprovedSpendingPlanDropDownList.DataValueField = "SpendingPlanId"
        ApprovedSpendingPlanDropDownList.DataBind()

    End Sub

    Protected Sub ExpenditureReportGridView_RowCommand(ByVal sender As Object, _
                                                       ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) _
                                                       Handles ExpenditureReportGridView.RowCommand

        If IsPostBack Then
            Page.SetFocus(e.CommandSource)
        End If
        Dim gv As GridView = CType(sender, GridView)

        Dim gv2 As GridView = CType(e.CommandSource, Button).Parent.NamingContainer.FindControl("GridView6")
        gv2.AlternatingRowStyle.BackColor = Drawing.Color.FromName("#E8ECF4")
        Dim headerStyle As New Style
        headerStyle.BackColor = Drawing.Color.FromName("#949FB1")
        gv2.HeaderRow.ApplyStyle(headerStyle)

        If gv2.Visible Then
            gv2.Visible = False
        Else
            gv2.Visible = True
        End If

    End Sub

    Protected Sub ExpenditureReportGridView_RowDataBound(ByVal sender As Object, _
                                                         ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
                                                         Handles ExpenditureReportGridView.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim gv As GridView = e.Row.FindControl("GridView6")
            Dim details As Button = e.Row.FindControl("ToggleDetailsButton")
            Dim bf As New BoundField
            Dim sqlDataSource As New SqlDataSource
            Dim parmSpendingPlanId As New Parameter
            Dim parmBudgetCategoryId As New Parameter
            Dim args As New DataSourceSelectArguments

            Dim ExpenditureReportSummary As Cdm.DataCore.Budget.ExpenditureReportSummary = e.Row.DataItem

            'Dim connectionString As String = ConfigurationManager.ConnectionStrings("SpendingPlanData").ConnectionString
            Dim connectionString As String = EntityController.AppConnectString

            sqlDataSource.ConnectionString = connectionString
            sqlDataSource.SelectCommand = "spGetExpenditureReportItems"
            sqlDataSource.SelectCommandType = SqlDataSourceCommandType.StoredProcedure

            parmSpendingPlanId.Name = "SpendingPlanId"
            parmSpendingPlanId.Type = TypeCode.Int32
            parmSpendingPlanId.DefaultValue = ExpenditureReportSummary.SpendingPlanId

            parmBudgetCategoryId.Name = "BudgetCategoryId"
            parmBudgetCategoryId.Type = TypeCode.Int32
            'parmBudgetCategoryId.DefaultValue = Session("BudgetCategoryId")
            parmBudgetCategoryId.DefaultValue = ExpenditureReportSummary.BudgetCategoryId

            sqlDataSource.SelectParameters.Add(parmSpendingPlanId)
            sqlDataSource.SelectParameters.Add(parmBudgetCategoryId)
            If Not Me.MatchSpendingPlanDateRange.Checked Then
                Dim parmStartDate As New Parameter
                Dim parmEndDate As New Parameter

                parmStartDate.Name = "StartDate"
                parmStartDate.Type = TypeCode.DateTime
                parmStartDate.DefaultValue = Me.StartDateInputControl.Value

                parmEndDate.Name = "EndDate"
                parmEndDate.Type = TypeCode.DateTime
                parmEndDate.DefaultValue = Me.EndDateInputControl.Value

                sqlDataSource.SelectParameters.Add(parmStartDate)
                sqlDataSource.SelectParameters.Add(parmEndDate)

            End If
            'Response.Write(ExpenditureReportSummary.BudgetCategoryId)
            'Response.Write(ExpenditureReportSummary.SpendingPlanId)
            'Response.End()
            Dim dv As Data.DataView = sqlDataSource.Select(args)

            gv.DataSource = sqlDataSource
            gv.AutoGenerateColumns = True

            If dv.Table.Rows(0).ItemArray.Length = 1 Then
                details.Visible = False
            End If

            gv.DataBind()
            gv.AlternatingRowStyle.BackColor = Drawing.Color.FromName("#E8ECF4")
            Dim headerStyle As New Style
            headerStyle.BackColor = Drawing.Color.FromName("#949FB1")
            gv.HeaderRow.ApplyStyle(headerStyle)
            gv.Visible = False

        End If

    End Sub

    Protected Sub EditCaseSubNav2_ChangeMenu(ByVal sender As Object, _
                                             ByVal e As System.EventArgs) _
                                             Handles EditCaseSubNav2.ChangeMenu

        EntityController.ReturnToCasePersonalPage(Me.EditCaseSubNav2)

    End Sub

    Protected Sub ShowBalance()
        Dim amount As Double
        Try
            lblBalance = ExpenditureReportGridView.FooterRow.FindControl("lblBalance")
            amount = GetAmount()
            lblBalance.Text = String.Format("{0:C}", amount)
        Catch
        End Try
    End Sub


    Private Function GetAmount() As Double

        Dim sqlDataSource As New SqlDataSource
        Dim connectionString As String = EntityController.AppConnectString
        Dim parmParticipantId As New Data.SqlClient.SqlParameter
        Dim parmSpendingPlanId As New Data.SqlClient.SqlParameter
        Dim args As New DataSourceSelectArguments
        Dim sqlDataAdapter As New System.Data.SqlClient.SqlDataAdapter()
        Dim command As New Data.SqlClient.SqlCommand()
        Dim connection As New Data.SqlClient.SqlConnection()

        command.Connection = connection
        command.Connection.ConnectionString = connectionString
        command.CommandText = "spGetExpenditureReportSummary"
        command.CommandType = System.Data.CommandType.StoredProcedure

        parmParticipantId.ParameterName = "ParticipantId"
        parmParticipantId.SqlDbType = Data.SqlDbType.Int


        parmParticipantId.Value = EntityController.GetParticipant().ParticipantId

        parmSpendingPlanId.ParameterName = "SpendingPlanId"
        parmSpendingPlanId.SqlDbType = Data.SqlDbType.Int
        If ApprovedSpendingPlanDropDownList.SelectedValue Is Nothing Then
            parmSpendingPlanId.Value = 0
        Else
            If ApprovedSpendingPlanDropDownList.SelectedValue = "" Then
                parmSpendingPlanId.Value = 0
            Else
                parmSpendingPlanId.Value = ApprovedSpendingPlanDropDownList.SelectedValue
            End If
        End If

        command.Parameters.Add(parmParticipantId)
        command.Parameters.Add(parmSpendingPlanId)

        sqlDataAdapter.SelectCommand = command

        Dim ds As New System.Data.DataSet()

        sqlDataAdapter.Fill(ds)

        If ds.Tables(1).Rows(0)("Total").Equals(System.DBNull.Value) Then
            Return 0.0
        Else
            Return Convert.ToDouble(ds.Tables(1).Rows(0)("Total").ToString())
        End If


    End Function

    Protected Sub Go_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Go.Click
        GetActivity()
    End Sub

    Private Sub GetActivity()
        Dim participantid As Integer = EntityController.GetParticipant().ParticipantId
        Dim spendingPlanId As Integer = Convert.ToInt32(ApprovedSpendingPlanDropDownList.SelectedValue)
        Dim summaryList As New System.Collections.Generic.List(Of Cdm.DataCore.Budget.ExpenditureReportSummary)
        If Me.MatchSpendingPlanDateRange.Checked Then
            summaryList = Cdm.DataCore.Budget.ExpenditureReportSummary.Retrieve(participantid, spendingPlanId)
            Me.ExpenditureReportGridView.DataSource = summaryList
            Me.ExpenditureReportGridView.DataBind()
            Me.ExpenditureReportGridView.Visible = True
        Else
            If String.IsNullOrEmpty(Me.StartDateInputControl.Value) Or String.IsNullOrEmpty(Me.EndDateInputControl.Value) Then
                ParticipantNameLabel.Text = "Please Select a date range, or check the box to select all activity for the spending plan."
                ParticipantNameLabel.Visible = True
                Me.ExpenditureReportGridView.Visible = False
            Else
                Dim startDate As DateTime = Convert.ToDateTime(Me.StartDateInputControl.Value)
                Dim endDate As DateTime = Convert.ToDateTime(Me.EndDateInputControl.Value)
                summaryList = Cdm.DataCore.Budget.ExpenditureReportSummary.Retrieve(participantid, spendingPlanId, startDate, endDate)
                Me.ExpenditureReportGridView.DataSource = summaryList

                Me.ExpenditureReportGridView.DataBind()
                Me.ExpenditureReportGridView.Visible = True
            End If
        End If

        If Me.ExpenditureReportGridView.Rows.Count > 0 And ExpenditureReportGridView.Visible Then
            ShowBalance()
            If Not summaryList Is Nothing Then
                SetLabels(spendingPlanId, summaryList)
            End If
        Else
            Me.ParticipantNameLabel.Visible = True
            Me.SpendingPlanAmountLabel.Visible = False
            Me.BudgetAmountLabel.Visible = False
            Me.TotalSpendingLabel.Visible = False
        End If
    End Sub

    Private Sub SetLabels(ByVal spendingPlanId As Integer, ByVal summaryList As System.Collections.Generic.List(Of Cdm.DataCore.Budget.ExpenditureReportSummary))
        Dim noSpending As Boolean = False
        Dim totalSpending As Double = 0
        If (summaryList Is Nothing) Then
            noSpending = True
        Else
            If summaryList.Count = 0 Then
                noSpending = True
            Else
                For i As Integer = 0 To summaryList.Count - 1
                    totalSpending = totalSpending + summaryList(i).TotalAmount
                Next
            End If
        End If

        Me.TotalSpendingLabel.Text = "Total Spending: " + totalSpending.ToString("C")
        Me.TotalSpendingLabel.Visible = True
        Dim currPlan As Cdm.DataCore.Budget.ApprovedSpendingPlan = Cdm.DataCore.Budget.ApprovedSpendingPlan.Retrieve(spendingPlanId)
        Me.BudgetAmountLabel.Text = "Budget Amount: " + currPlan.BudgetAmount.ToString("C") + " " + currPlan.BudgetPeriodDescription
        Me.BudgetAmountLabel.Visible = True
        Me.SpendingPlanAmountLabel.Text = "Spending Plan Amount: " + currPlan.SpendingPlanAmount.ToString("C")
        Me.SpendingPlanAmountLabel.Visible = True
    End Sub

End Class
